package threadDemo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 使用多线程下载网络图片 导入commonsIo包
 */
public class Test6  extends Thread{
    private String url;
    private String name;

    public Test6(String url, String name) {
        this.url = url;
        this.name = name;
    }


    public static void main(String[] args) {
        Test6 t = new Test6("https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E5%BA%93&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=2043261133,3401786105&os=4172740050,89812164&simid=4013274275,482206620&pn=0&rn=1&di=181500&ln=1963&fr=&fmq=1625552030586_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fpic29.nipic.com%252F20130508%252F9252150_134600220000_2.jpg%26refer%3Dhttp%253A%252F%252Fpic29.nipic.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1628144035%26t%3Df2d5eeec07162a1920f238628bc8070d&rpstart=0&rpnum=0&adpicid=0&nojc=undefined","pic1");
        Test6 t1 = new Test6("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E5%BA%93&step_word=&hs=0&pn=1&spn=0&di=167860&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=2639104577%2C804949385&os=2266015141%2C2569162708&simid=0%2C0&adpicid=0&lpn=0&ln=1963&fr=&fmq=1625552030586_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fpic3.nipic.com%2F20090523%2F2031441_113202079_2.jpg%26refer%3Dhttp%3A%2F%2Fpic3.nipic.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1628144093%26t%3Dcfc7090db8bae9471ca5234f24b73e95&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bgtrtv_z%26e3Bv54AzdH3Ffi5oAzdH3F8bmmbca_z%26e3Bip4s&gsm=2&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined","pic2");
       Test6 t2 = new Test6("https://pics6.baidu.com/feed/0b46f21fbe096b6301195f71c5c6d542eaf8ac8f.jpeg?token=a81eb1c51ba26251468885e01f39e4f0&s=FAE0D54A5AE4B7644E75E40F0000E0C3","pic3");
        t.start();
        t1.start();
        t2.start();
    }

    @Override
    public void run() {

        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloadPic(url,name);
        System.out.println("下载的文件名为： "+name);
    }
}
class WebDownloader{
    public  void downloadPic(String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            System.out.println("downloadPic方法有异常");
        }
    }
}
